<template>
    <div>
        <h3>优惠劵显示</h3>
        标题:
        <input type="text" v-model="queryinfo.Title"/>
        时间
        <input type="date" v-model="queryinfo.sdate"/>- <input type="date" v-model="queryinfo.edate"/>
        <input type="button" value="查询" class="btn btn-info" @click="show"/>
        <input type="button" value="批量发布" class="btn btn-warning" @click="updstate"/>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td><input type="checkbox" v-model="check" ></td>
                    <td>ID</td>
                    <td>标题</td>
                    <td>优惠类型</td>
                    <td>副标题</td>
                    <td>图片</td>
                    <td>库存</td>
                    <td>限领数量</td>
                    <td>消费金额</td>
                    <td>减免金额</td>
                    <td>折扣价</td>
                    <td>开始时间</td>
                    <td>结束时间</td>
                    <td>适应业务</td>
                    <td>使用须知</td>
                    <td>门店外键</td>
                    <td>服务电话</td>
                    <td>优惠劵颜色</td>
                    <td>状态</td>
                    <td>操作</td>
                </tr>
                <tr v-for="item in data.list" :key="item.Id">
                    <td><input type="checkbox" v-model="ids" :value="item.Id"/></td>
                    <td>{{ item. Id}}</td>
                    <td>{{ item.Title }}</td>
                    <td>
                        <span v-if="item.Discon==1">代金券</span>
                        <span v-if="item.Discon==2">折扣券</span>
                        <span v-if="item.Discon==3">兑换券</span>
                    </td>
                    <td>{{ item.Subtile }}</td>
                    <td><img :src="item.Photo" alt="" width="100" height="100"/></td>
                    <td>{{ item.Count }}</td>
                    <td>{{ item.RersonNumber }}</td>
                    <td>{{ item.AmountMonty }}</td>
                    <td>{{ item.SubMonty }}</td>
                    <td>{{ item.Zhekou }}</td>
                    <td>{{ item.BeginTime.substring(0,10) }}</td>
                    <td>{{ item.EndTime.substring(0,10) }}</td>
                    <td>{{ item.Uservice }}</td>
                    <td>{{ item.UserDesc }}</td>
                    <td>{{ item.ShopName }}</td>
                    <td>{{ item.Phone.substring(0,3)+'****'+item.Phone.substring(7) }}</td>
                    <td>{{ item.Color }}</td>
                    <td>{{ item.State==1?"发布":"不发布" }}</td>
                    <td>
                        <input type="button" value="修改" class="btn btn-success" @click="update(item.Id)"/>
                        <input type="button" value="删除" class="btn btn-danger" @click="del(item.Id)"/>
                    </td>
                </tr>
            </tbody>
        </table>
        <div>
            <span>共{{ data.tocount }}条</span>
            <span>共{{ data.topage }}页</span>
            <span>每页{{ queryinfo.pagesize }}条</span>
            <span>当前是{{ queryinfo.pageindex }}/{{data.topage}}页</span>
            <select v-model="queryinfo.pagesize" @change="show">
                <option :value="item" v-for="item in arr" :key="item">每页{{ item }}条</option>
            </select>
            <input type="button" value="首页" @click="fanye(1)"/>
            <input type="button" value="上一页" @click="fanye(queryinfo.pageindex-1)"/>
            <input type="button" value="下一页" @click="fanye(queryinfo.pageindex+1)"/>
            <input type="button" value="尾页" @click="fanye(data.topage)"/>
        </div>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive, onMounted}from 'vue';
import axios from 'axios';
import moment from 'moment';
import {useRouter,useRoute}from 'vue-router'
const router=useRouter();
const route=useRoute();

let queryinfo:any=reactive({
    pageindex:1,
    pagesize:2,
    Title:'',
    sdate:'',
    edate:''
})

let data:any=reactive({
    list:[],
    tocount:0,
    topage:0
})

let check=ref(false);
let ids=ref([]);

let arr=ref([2,5,7,9]);

onMounted(()=>{
    getdist();
})


const updstate=()=>{
    if(!confirm("确认修改吗")){
        return;
    }
    axios({
        url:'/api/Dist/UpPiliang',
        method:'get',
        params:{ids:ids.value.toString()},
    })
    .then(res=>{
        console.log(res);
        if(res.data>0){
            alert('修改成功');
            check.value=false;
            ids.value=[];
            getdist();
        }
        else{
            alert('修改失败');
            check.value=false;
            ids.value=[];
        }
    })
    .catch(err=>{
        console.log(err);
    })
}

const show=()=>{
    queryinfo.pageindex=1;
    getdist();
}

const update=(id:any)=>{
    router.push({
        path:'/upd',
        query:{id:id}
    })
}
const del=(id:any)=>{
    if(!confirm("确认删除吗"))
{
    return;
}
    axios({
        url:'/api/Dist/Del',
        method:'delete',
        params:{id:id}
    })
    .then(res=>{
        console.log(res);
        if(res.data>0){
            alert('删除成功');
            getdist();
        }
        else{
            alert('删除失败');
        }
    })
    .catch(err=>{
        console.log(err);
    })
}

const fanye=(num:any)=>{
    if(num<1){
        queryinfo.pageindex=1;
        return;
    }
    if(num>data.topage){
        queryinfo.pageindex=data.topage;
        return;
    }
    queryinfo.pageindex=num;
    getdist();
}
const getdist=()=>{
    axios({
        url:'/api/Dist/GetDist',
        method:'get',
        params:queryinfo
    })
    .then(res=>{
        console.log(res);
        data.list=res.data.c;
        data.tocount=res.data.tocount;
        data.topage=res.data.topage;
    })
    .catch(err=>{
        console.log(err);
    })
}
</script>

<style scoped>
span{
    margin-left: 10px;
}
input[type="button"]{
    margin-left: 20px;
}
</style>